package com.five.controller;

import com.five.pojo.ClubActivity;
import com.five.pojo.PageBean;
import com.five.pojo.Result;
import com.five.pojo.ClubRegister;
import com.five.service.ClubActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 */
@Tag(name = "社团活动接口")
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/clubActivity")
public class ClubActivityController {
    @Autowired
    ClubActivityService clubActivityService;
    //获取所有活动信息
    @Operation(summary = "获取所有活动信息(学生)")
    @GetMapping("/getAll")
    public Result getAll(){
        List<ClubActivity> list = clubActivityService.getAllActivity();
        return Result.success(list);
    }
    //分页
    @Operation(summary = "按条件查询分页")
    @GetMapping("page")
    public Result page(@RequestParam(defaultValue = "1") Integer page ,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                        String actName)
    {
        PageBean pageBean = clubActivityService.page(page , pageSize , actName);
        return Result.success(pageBean);
    }

    //报名活动
    @Operation(summary = "报名参加活动(学生)")
    @PostMapping("/add")
    public Result add(@RequestBody ClubRegister clubRegister){
        String sName = clubActivityService.findByName(clubRegister.getSName());
        if(sName == null){
            clubActivityService.addRegisterInfo(clubRegister);
        }else {
            return Result.error("您已经报名，请勿重复报名");
        }
        return Result.success();
    }

    //团长审批学生活动报名信息
    @Operation(summary = "团长审批学生活动报名信息接口")
    @PutMapping("/ModStatus1")
    public Result ModStatus1(Integer status , Integer id){
        clubActivityService.ModStatus1(status , id);
        return Result.success();
    }

    //团长：删除活动
    @Operation(summary = "删除活动(团长)")
    @DeleteMapping
    public Result delete(Integer id){
        clubActivityService.deleteById(id);
        return Result.success();
    }

    //团长发布社团活动
    @Operation(summary = "发布社团活动(团长)")
    @PostMapping("/addActivity")
    public Result addActivity(@RequestBody ClubActivity clubActivity){
        System.out.println(clubActivity.getActBeginTime());
        // 解析日期字符串到ZonedDateTime对象
        ZonedDateTime ActBeginTime = ZonedDateTime.parse(clubActivity.getActBeginTime());
        ZonedDateTime ActTime = ZonedDateTime.parse(clubActivity.getActTime());
        // 创建格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间
        String formattedDateTime = ActBeginTime.format(formatter);
        String formattedDateTime1 = ActTime.format(formatter);
        // 输出结果
        System.out.println(formattedDateTime);
        System.out.println(formattedDateTime1);
        clubActivity.setActBeginTime(formattedDateTime);
        clubActivity.setActTime(formattedDateTime1);
        clubActivityService.addActivity(clubActivity);
        return Result.success();
    }

    //管理员审批团长发布的活动信息
    @Operation(summary = "管理员审批新发布活动接口(管理员)")
    @PutMapping("/ModStatus")
    public Result ModStatus(Integer status , Integer id){
        clubActivityService.ModStatus(status , id);
        if(status==1){
            return Result.success(1);
        }
        return Result.success(0);
    }

    //查看社团活动数量
    @Operation(summary = "查看社团活动总数")
    @GetMapping("/getClubActivityTotal")
    public Result getClubActivityTotal(){
        Integer total = clubActivityService.getClubActivityTotal();
        return Result.success(total);
    }

    //团长根据社团名称查询本社团活动
    @Operation(summary = "根据社团名称查询")
    @GetMapping("/getByActClub")
    public Result getByActClub(String actClub){
        List<ClubActivity> clubActivityList = clubActivityService.getByActClub(actClub);
        return Result.success(clubActivityList);
    }

    //团长修改活动
    @Operation(summary = "团长修改本社团活动")
    @PutMapping("/update")
    public Result updateActivity(@RequestBody ClubActivity clubActivity){
        // 解析日期字符串到ZonedDateTime对象
        ZonedDateTime actBeginTime = ZonedDateTime.parse(clubActivity.getActBeginTime());
        ZonedDateTime ActTime = ZonedDateTime.parse(clubActivity.getActTime());
        // 创建格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间
        String formattedDateTime = actBeginTime.format(formatter);
        String formattedDateTime1 = ActTime.format(formatter);
        // 输出结果
        System.out.println(formattedDateTime);
        System.out.println(formattedDateTime1);
        clubActivity.setActBeginTime(formattedDateTime);
        clubActivity.setActTime(formattedDateTime1);
        clubActivityService.updateActivity(clubActivity);
        return Result.success();
    }
}
